package com.itheima.day15多线程;

/*3、【编号2003】某电影院目前正在上映国产大片，
共有100张票，而它有3个窗口卖票，
请设计一个程序模拟该电影院卖票，
并保证线程同步，不会出现错误的买票数据。*/
public class Demo3 implements Runnable {
    int count = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (Demo3.class) {
                if (count == 0) {
                    System.out.println("没票了");
                    break;
                } else {
                    count--;
                    System.out.println(Thread.currentThread().getName() + "还剩" + count + "张票");
                }
            }
        }
    }
}

class Test3 {
    public static void main(String[] args) {
        Demo3 d = new Demo3();
        Thread t1 = new Thread(d, "线程一");
        Thread t2 = new Thread(d, "线程二");
        Thread t3 = new Thread(d, "线程三");
        t1.start();
        t3.start();
        t2.start();
    }
}
